// pages/buy/buy.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        avatarUrl: '',
        tel: null,
        money: 160,
        time: 60,
        disabled: false,
        codeShow: true,
        code: null,
        company: null,
    },
    // 用户输入的手机号
    getTelephone(e) {
        this.setData({
            tel: e.detail.value
        })
    },
    // 用户输入的验证码
    getCode(e) {

        this.setData({
            code: e.detail.value
        })
    },
    // 用户输入的公司
    getCompany(e) {
        this.setData({
            company: e.detail.value
        })
    },
    toCode() {
        console.log('点击发送验证码按钮了');
        var _this = this;
        this.setData({
            codeShow: false,
            disabled: true,
            time: _this.data.time - 1
        })

        if (_this.data.time) {
            var _timer = setInterval(function () {
                _this.setData({
                    time: --_this.data.time
                })
                if (_this.data.time < 10) {
                    _this.setData({
                        time: '0' + _this.data.time
                    })
                }
                if (_this.data.time == 0) {
                    console.log('到0了');
                    clearInterval(_timer)
                    _this.setData({
                        time: 60,
                        disabled: false,
                        codeShow: true
                    })
                }
                // console.log(_this.data.time);
            }, 1000)
        }
        // var openid = wx.getStorageSync('openid');
        wx.request({
            url: app.globalData.url + 'Login/telCode',
            method: 'POST',
            data: {
                openid: wx.getStorageSync('openid'),
                telephone: _this.data.tel,
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
                console.log(res);
                // if (res.data.status === 1) {
                    // 15172526816
                // }
                wx.showToast({
                    title: res.data.msg,
                    // icon: 'success',
                    duration: 2000
                })
            }

        })
    },
    toRegister() {
        var that = this;
        wx.request({
            url: app.globalData.url + 'Login/register',
            data: {
                openid: wx.getStorageSync('openid'),
                telephone: that.data.tel,
                code: that.data.code,
                company: that.data.company
            },
            method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
                console.log(res);
                if(res.data.status === 1) {
                    wx.showToast({
                        title: res.data.msg,
                        // icon: 'success',
                        duration: 2000
                    })
                    wx.navigateTo({
                        url: '../mycard/mycard',
                    })
                } else if(res.data.status === 0) {
                    wx.showToast({
                        title: res.data.msg,
                        // icon: 'success',
                        duration: 2000
                    })
                } 
            },
            fail: function () {
                // fail
            },
            complete: function () {
                // complete
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            avatarUrl: app.globalData.userInfo.avatarUrl
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})